<?php
/**
 * 菜单控制器
 * User: linhui
 * Date: 2016/12/14
 * Time: 2:57
 */

namespace Admin\Controller;


use Admin\Model\MenuModel;

class MenuController extends BaseController
{
    /**
     *菜单列表
     */
    public function index()
    {
        $model = D('menu');
        //排序
        if(IS_POST){
            $this->sort($model);
        }
        $menuList = $model->getMenuListOrderByParentId();
        $this->assign('list', $menuList);
        $this->display();
    }
    /**
     * 菜单编辑前操作
     * @param $model
     * @param $info
     */
    public function beforeEdit($model, &$info)
    {
        $model = new MenuModel();
        $this->assign('allMenu', $model->getMenuList(0, true));
    }

    /**
     *配置权限
     */
    public function setting()
    {
        $model = D('auth');
        $menu_id = I('menu_id', 0, 'intval');
        //菜单
        $menu = D('menu')->find($menu_id);
        //提交数据处理
        if(IS_POST){
            $data = I('post.');
            $add = array();
            $time = time();
            for($i = 0; $i < count($data['auth_name']); $i++){
                if($data['auth_name'][$i] && $data['module_name'][$i] && $data['controller_name'][$i] && $data['action_name'][$i]){
                    $map = array(
                        'menu_id'=>$menu_id,
                        'module_name'=>$data['module_name'][$i],
                        'controller_name'=>$data['controller_name'][$i],
                        'action_name'=>$data['action_name'][$i],
                    );
                    if($model->where($map)->find()){
                        $model->where($map)->save(array('update_time'=>$time, 'remark'=>$data['remark'][$i]));
                        continue;
                    }
                    $add[] = array(
                        'auth_name'=>$data['auth_name'][$i],
                        'module_name'=>$data['module_name'][$i],
                        'controller_name'=>$data['controller_name'][$i],
                        'action_name'=>$data['action_name'][$i],
                        'remark'=>$data['remark'][$i],
                        'menu_id'=>$menu_id,
                        'update_time'=>$time,
                        'create_time'=>$time,
                        );
                }
            }
            if($model->addAll($add) > 0 || count($add) == 0){

                $map = array(
                    'update_time'=>array('lt', $time),
                    'menu_id'=>$menu_id,
                );
                $model->where($map)->delete();
                //权限
                $map = array('menu_id'=>$menu_id);
                $indexAuth = $model->where(array_merge($map, array('action_name'=>'index')))->field('module_name, controller_name, action_name')->find();
                $auth = $indexAuth ? $indexAuth : $model->where($map)->order('create_time')->field('module_name, controller_name, action_name')->find();
                $url = join('/', array_values($auth));
                D('menu')->where(array('menu_id'=>$menu_id))->save(array('url'=>$url));
                $this->responseAjax('保存成功', true, array(), U('setting', 'menu_id='.$menu_id));
            }
            $this->responseAjax('保存失败', false);
        }

        //权限
        $map = array('menu_id'=>$menu_id);
        $authList = $model->where($map)->order('create_time')->select();

        $this->assign('list', $authList);
        $this->assign('menu', $menu);
        $this->display();
    }
}